package top.yeyusmile.service;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yeyusmile.common.MyTemplate;
import top.yeyusmile.mirai.Sender;
import top.yeyusmile.mirai.msg.ImageMsg;
import top.yeyusmile.mirai.msg.PlainMsg;
import top.yeyusmile.pojo.*;
import top.yeyusmile.utils.HttpUtil;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @author 夜雨
 * @Web www.yeyusmile.top
 * @date 2022/3/16 15:15
 */
@Slf4j
@Service
public class ConvenientServiceImpl implements ConvenientService {

    @Autowired
    private MyTemplate myTemplate;

    //private MyQQMessage myQQMessage;


//    @Override
//    public void historyToday(MyQQMessage message) {
//        //this.myQQMessage = message;
//        String url = "https://api.heroa.cn:3403/convenience/history_today/";
//        HttpUtil.httpsGet(url,"", new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//                log.info(e.toString());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                // System.out.println(response.body().string());
//                HistoryTody historyTody = new Gson().fromJson(response.body().string(), HistoryTody.class);
//                sendMsgService.sendMsg(historyTody.toString(), message);
//            }
//        });
//        /*try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }*/
//    }

//    @Override
//    public synchronized void SixtyReadWorld(MyQQMessage message) {
//        //this.myQQMessage = message;
//        String url = "https://api.heroa.cn:3403/convenience/sixty_second_read_world/";
//        HttpUtil.httpsGet(url,"", new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//                log.info(e.toString());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                // System.out.println(response.body().string());
//                SixtyReadWorld sixtyReadWorld = new Gson().fromJson(response.body().string(), SixtyReadWorld.class);
//                sendMsgService.sendMsg(sixtyReadWorld.toString(), message);
//            }
//        });
//    }


    @Override
    public void diary(Sender sender) {
        //this.myQQMessage = message;
        String url = "https://api.heroa.cn:3403/random/diary/";
        HttpUtil.httpsGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // System.out.println(response.body().string());
                CommonResult commonResult = new Gson().fromJson(response.body().string(), CommonResult.class);
                List<Object> params = new ArrayList<>();
                params.add(new PlainMsg(commonResult.toString()));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());
            }
        });
    }

    @Override
    public synchronized void quotation(Sender sender) {
        //this.myQQMessage = message;
        String url = "https://api.heroa.cn:3403/random/quotation/";
        HttpUtil.httpsGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // System.out.println(response.body().string());
                CommonResult commonResult = new Gson().fromJson(response.body().string(), CommonResult.class);
                List<Object> params = new ArrayList<>();
                params.add(new PlainMsg(commonResult.toString()));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());

            }
        });
    }

    @Override
    public void lookGirl(Sender sender, String keyword) {


        int st = new Random(System.currentTimeMillis()).nextInt(30) * 10;
        String url = "https://pic.sogou.com/napi/pc/searchList?mode=1&start=" + st + "&xml_len=48&query=%E7%BE%8E%E5%A5%B3";
        if (keyword == null || "".equals(keyword)) {
            url = "https://pic.sogou.com/napi/pc/searchList?mode=1&start=" + st + "&xml_len=48&query=%E7%BE%8E%E5%A5%B3";
        } else {
            url = "https://pic.sogou.com/napi/pc/searchList?mode=1&start=" + st + "&xml_len=48&query=" + URLEncoder.encode(keyword);
        }

        HttpUtil.httpsGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String resp = response.body().string();

                JsonParser jsonParser = new JsonParser();
                JsonElement element = jsonParser.parse(resp);
                JsonObject root = element.getAsJsonObject();
                int index = new Random(System.currentTimeMillis()).nextInt(47);
                //data.items[0].picUrl
                String picUrl = root
                        .getAsJsonObject("data").
                        getAsJsonArray("items")
                        .get(index).getAsJsonObject()
                        .get("picUrl").getAsString();

                List<Object> params = new ArrayList<>();
                params.add(new ImageMsg(picUrl));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());

            }
        });
    }

    @Override
    public void qqLucky(String qq, Sender sender) {
        //this.myQQMessage = message;
        String url = "http://qxu66.xyz/api/qq_xj.php?qq=" + qq;
        HttpUtil.httpGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();
                QQLucky qqLucky = new Gson().fromJson(retStr, QQLucky.class);

                List<Object> params = new ArrayList<>();
                params.add(new PlainMsg(qqLucky.getData().toString()));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());
            }
        });
    }

    @Override
    public void raokouling(Sender sender) {
        //this.myQQMessage = message;
        String url = "http://qxu66.xyz/api/rkl.php";
        HttpUtil.httpGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();

                List<Object> params = new ArrayList<>();
                params.add(new PlainMsg(retStr));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());

            }
        });
    }

    @Override
    public void joke(Sender sender) {
        //this.myQQMessage = message;
        String url = "http://111.229.174.142:12345/query=&type=xiaohua";
        HttpUtil.httpGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();
                List<Object> params = new ArrayList<>();
                params.add(new PlainMsg(retStr));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());
            }
        });
    }

    @Override
    public void duanzi(Sender sender) {
        //this.myQQMessage = message;
        String url = "http://111.229.174.142:12345/query=&type=duanzi";
        HttpUtil.httpGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String retStr = response.body().string();
                List<Object> params = new ArrayList<>();
                params.add(new PlainMsg(retStr));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());

            }
        });
    }

    @Override
    public void ocrPic(Sender sender, String url) {

    }


//    @Override
//    public void weather(String weather, MyQQMessage message) {
//        //this.myQQMessage = message;
//        String url = null;
//        try {
//            url = "http://111.229.174.142:12345/query=" + URLEncoder.encode(weather, "utf-8") + "&type=weather1";
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//
//        HttpUtil.httpGet(url,"", new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//                log.info(e.toString());
//            }
//
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                String retStr = response.body().string().replaceAll("var dataSK=", "");
//                if (!"失败".equals(retStr)) {
//                    Weather w = new Gson().fromJson(retStr, Weather.class);
//                    sendMsgService.sendMsg(w.toString(), message);
//                }
//            }
//        });
//    }

    @Override
    public void lookAnime(Sender sender) {
        //this.myQQMessage = message;
        String url = "https://api.mtyqx.cn/api/random.php";
        HttpUtil.httpsGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                //     String picUrl = response.header("location");
                //   String picUrl = response.body().string();
                String picUrl = response.request().url().toString();
                List<Object> params = new ArrayList<>();
                params.add(new ImageMsg(picUrl));
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());

            }
        });
    }


    @Override
    public void chat(Sender sender, String msg) {
        String url = "https://api.ownthink.com/bot?appid=cddee40e28accae0105469030a4d7a51&userid=7Gl7xYps&spoken=" + URLEncoder.encode(msg);

        HttpUtil.httpsGet(url, "", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                log.info(e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                //     String picUrl = response.header("location");
                //   String picUrl = response.body().string();

                String retStr = response.body().string();
                JsonParser jsonParser = new JsonParser();
                JsonElement element = jsonParser.parse(retStr);
                JsonObject root = element.getAsJsonObject();
                String text = root
                        .getAsJsonObject("data").
                        getAsJsonObject("info")
                        .get("text").getAsString();
                List<Object> params = new ArrayList<>();
                params.add(new PlainMsg(text));
                log.info("聊天：{}", params);
                myTemplate.sendMsg2Group(params, null, sender.getGroup().getId());

            }
        });
    }
}
